package com.atguigu.eduorder.controller;


import com.atguigu.common.R;
import com.atguigu.eduorder.service.TPayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author Panghl
 * @since 2021-02-21
 */
@RestController
@RequestMapping("/eduorder/t-pay-log")
@Api(tags = {"订单支付日志管理"})
public class TPayLogController {

    @Autowired
    private TPayLogService payLogService;

    @ApiOperation(value = "生成二维码")
    @GetMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable("orderNo") String orderNo){
        //返回信息，包含二维码地址，还有其他需要的信息
        Map<String,Object> map = payLogService.createNative(orderNo);
        System.out.println("***返回二维码map集合***:"+map);
        return R.ok().data(map);
    }

    @ApiOperation(value = "获取查询订单状态")
    @GetMapping("/queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable("orderNo") String orderNo){
        Map<String,String> map = payLogService.queryPayStatus(orderNo);
        System.out.println("***获取查询订单map集合***:"+map);
        if (map == null){
            return R.error().message("支付出错了");
        }
        if (map.get("trade_state").equals("SUCCESS")){
            //支付成功,修改订单状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.error().code(25000).message("支付中...");
    }
}
